<?php
/**
 * Users controller class
 *
 */
class StudController extends Admin_Controller
{
    private $table_gallery;
    private $table_cities;

	public function init()
    {
    	parent::init();
    	$this->_table = &$this->table_accounts;
    	//$this->table_gallery = new Admin_Table_Gallery();
    	$this->table_cities = new Admin_Table_Auction_Cities();
    	$this->setActiveCat(93);
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {        
        $this->page_type = 'list';

    	$reset = $this->_request->getParam('reset', 0);

    	$form = new Llib_Form($this->_request);
    	$data = array();

        if ($this->_request->isPost())
    	{
	    	$data['is_active'] = $this->_getParam('is_active', -1);
                //$data['is_online'] = $this->_getParam('is_online', -1);
	    	$data['role_id'] = $this->_getParam('role_id', -1);
	    	$data['search_text'] = $this->_getParam('search_text', '');
	    	$data['id_region'] = $this->_getParam('id_region', -1);
	    	$data['id_district'] = $this->_getParam('id_district', -1);
	    	$data['id_city'] = $this->_getParam('id_city', -1);
    	}

        $data = $this->SessionVars($data, array('role_id', 'is_active', 'is_online', 'search_text', 'id_region', 'id_city', 'id_district'), $reset);

        
    	$lclm = new Llib_List($this->_request, $this->_sess);
    	$lclm->AddParams(array(
    		'checkboxes' => true,
			//'link_fields' => 'last_name',
    		'link' => "/" . $this->controller . "/"//'/accounts'
    	));
    	//$lclm->AddColumn('Логин', 'username', array('width' => '20%', 'sort' => 1));
    	$lclm->AddColumn('Прізвище', 'last_name', array('width' => '30%', 'sort' => 1));
    	$lclm->AddColumn("Ім'я", 'first_name', array('width' => '30%', 'sort' => 1));
    	//$lclm->AddColumn('Логін', 'username', array('width' => '20%', 'sort' => 1));
    	$lclm->AddColumn('Роль', 'role_name', array('width' => '40%'));


    	$lclm->AddFunc();

    	$data['role'] = $this->_user->role_id;
    	$lclm->AddData($this->_table->GetDataList($lclm->page, $lclm->s, $lclm->pager_count, $data));

        $id_region = intval(@$data['id_region']);
        $id_district = intval(@$data['id_district']);
        $id_city = intval(@$data['id_city']);
        
        $data['id_region'] = $this->table_cities->GetRegionsList($id_region);
        $data['id_district'] = $this->table_cities->GetDistrictsList($id_district, $id_region);
        $data['id_city'] = $this->table_cities->GetCitiesList($id_city, $id_region, $id_district);

        $data['role_id'] = $this->_table->GetRolesList(intval(@$data['role_id']));
        $data['is_active'] = $form->GetActiveList(intval(@$data['is_active']));


        $form->AddData($data);

        $this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('List', $lclm->GetList());
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	$this->view->xmlWriter->endElement();
    }


    public function editAction()
    {
    	$id = $this->_request->getParam('id');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$data = $this->_table->GetDataRow($id);

    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost())
        {
                $d = $form->GetPostedData(array('add_phone'));
//                if($this->_user->id == $d['id']){
//                    $auth = Zend_Registry::get('auth');                 
//                    $stdData = (object)$d;
//                    $role = $this->table_roles->GetRoleByID($stdData->role_id);
//                    $stdData->role_name = $role['name'];
//                    $auth->getStorage()->write($stdData);
//                }
        	if($this->_table->update($d, $id, $this->_user->id)) $form->SetSuccess();
        	else $form->SetError($this->_table->GetError());
        }

        $data = $this->_table->GetDataRow($id);
        $data['role_id'] = $this->_table->GetRolesList($data['role_id'], $this->_user->role_id);

        $id_region = $data['id_region'];
        $id_district = $data['id_district'];
        $id_city = $data['id_city'];
        
        $data['id_region'] = $this->table_cities->GetRegionsList($id_region);
        $data['id_district'] = $this->table_cities->GetDistrictsList($id_district, $id_region);
        $data['id_city'] = $this->table_cities->GetCitiesList($id_city, $id_region, $id_district);

/*
	$id_cat = intval($data['id_cat']);
        $data['id_cat'] = $this->table_categories->GetCatList(array($id_cat), array('level' => 3, 'is_show' => 1, 'id_menu' => 1));

        if($id_cat > 0)
        $data['id_cats'] = $this->table_categories->GetCatList(@$data['id_cats'], array('parent_id' => $id_cat));
*/
        $form->AddData($data);

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $id);
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

//
//    public function changeroleAction()
//    {
//    	$id = $this->_request->getParam('id');
//    	if(!($id > 0)) return ;
//
//		$role_id  = intval($_REQUEST['id']);
//		$data = $this->_table->change_role($role_id, $id, $this->_user->id);
//
//    	die();
//    }

}
